﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;


namespace UFrame
{
    public static class EncryptionUtil
    {
        public static string Key = "sKkpirhi";
        public static string IV = "vViElW37";

        public static Encoding EncodingType = Encoding.ASCII;
        private static byte[] ByKey;
        private static byte[] ByIV;

        static EncryptionUtil()
        {
            ByKey = EncodingType.GetBytes(Key);
            ByIV = EncodingType.GetBytes(IV);
        }

        public static string Encode(string data)
        {
            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            int i = cryptoProvider.KeySize;
            MemoryStream ms = new MemoryStream();
            CryptoStream cst =
                new CryptoStream(ms, cryptoProvider.CreateEncryptor(ByKey, ByIV), CryptoStreamMode.Write);

            StreamWriter sw = new StreamWriter(cst);
            sw.Write(data);
            sw.Flush();
            cst.FlushFinalBlock();
            sw.Flush();
            return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
        }

        public static string Decode(string data)
        {
            byte[] byEnc;
            try
            {
                byEnc = Convert.FromBase64String(data);
            }
            catch
            {
                return null;
            }

            return Decode(byEnc);
        }

        public static string Decode(byte[] data)
        {
            byte[] byEnc = data;
            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream ms = new MemoryStream(byEnc);
            CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(ByKey, ByIV), CryptoStreamMode.Read);
            cst.FlushFinalBlock();
            StreamReader sr = new StreamReader(cst);
            return sr.ReadToEnd();
        }


        public static string Encode(byte[] data)
        {
            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            int i = cryptoProvider.KeySize;
            MemoryStream ms = new MemoryStream();
            CryptoStream cst =
                new CryptoStream(ms, cryptoProvider.CreateEncryptor(ByKey, ByIV), CryptoStreamMode.Write);
            cst.Write(data, 0, data.Length);
            cst.FlushFinalBlock();
            return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
        }


        public static string RSAEncrypt(string normaltxt)
        {
            var bytes = Encoding.Default.GetBytes(normaltxt);
            var encryptBytes = new RSACryptoServiceProvider(new CspParameters()).Encrypt(bytes, false);
            return Convert.ToBase64String(encryptBytes);
        }

        public static string RSADecrypt(string securityTxt)
        {
            try
            {
                var bytes = Convert.FromBase64String(securityTxt);
                var DecryptBytes = new RSACryptoServiceProvider(new CspParameters()).Decrypt(bytes, false);
                return Encoding.Default.GetString(DecryptBytes);
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        public static byte[] CustomEncode(byte[] b)
        {
            byte[] newB = new byte[b.Length];
            Array.Copy(b, newB, b.Length);
            for (int i = 0; i < newB.Length; i++)
            {
                newB[i]++;
            }

            return newB;
        }

        public static byte[] CustomDecode(byte[] b)
        {
            byte[] newB = new byte[b.Length];
            Array.Copy(b, newB, b.Length);
            for (int i = 0; i < newB.Length; i++)
            {
                newB[i]--;
            }

            return newB;
        }
    }
}